var fs = require("fs")//引入fs模块
var express = require("express")
const bodyParser = require("body-parser")
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))//解析表单数据
app.use(bodyParser.json())//解析json数据
//获取所有数据
var ALL_Book = fs.readFileSync("./book.json")
ALL_Book = ALL_Book.toString()
ALL_Book = JSON.parse(ALL_Book)
console.log(ALL_Book);
//中间件
app.use((req, res, next) => {
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    res.setHeader('Access-Control-Allow-Origin', "*")//允许跨域
    next()
})
app.get("/userbook", function (req, res) {
    res.json({
        code: 0,
        msg: "获取所有数据成功",
        data: ALL_Book
    })
})
app.post("/post", function (req, res) {
    res.json({
        code: 0,
        msg: "添加接口完成",
        data: req.body
    })
})
app.listen(3000, () => {
    console.log('启动服务；http://127.0.0.1:3000')
})
